<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 图表
 */
class Statistics extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * 用户产币图表
     * @return void
     */
    public function userHNT()
    {
        $user = $this->auth->getUser();
        $user_id = $user['id'];

        $data = ['time' => [], 'num' => []];

        $year = date('Y', time());
        for ($i = 0; $i < 6; $i++) {
            $data['time'][] = $year - $i;
            $data['num'][] = 0;
        }

        $data['time'] = array_reverse($data['time']);

        try {
            $start_time = date("Y-01-01 00:00:00", strtotime("-5 year"));
            $list = \app\common\model\HNTLog::where(['user_id' => $user_id, 'createtime' => ['> time', strtotime($start_time)]])->group("FROM_UNIXTIME(createtime ,'%Y')")->field("FROM_UNIXTIME(createtime,'%Y') `time`, sum(HNT) `num`")->select();
            foreach ($list as $item) {
                foreach ($data['time'] as $key => $datum) {
                    if ($item->time == $datum) {
                        $data['num'][$key] = $item->num;
                    }
                }
            }

        } catch (\Exception $e) {
        }

        $this->success(__('获取成功'), $data);
    }
}